package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.ProductListAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.ProductLedgerOriginal;
import com.isunland.managesystem.utils.LogUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by lenovo on 2016/4/21.
 */
public class ProductListFragment extends BaseListFragment {

  protected static final String EXTRA_PRODUCT_NAMES =
      "com.isunland.managesystem.ui.ProductListFragment.EXTRA_PRODUCT_NAMES";
  protected static final String EXTRA_PRODUCT_CODES =
      "com.isunland.managesystem.ui.ProductListFragment.EXTRA_PRODUCT_CODES";
  private ProductListAdapter mAdapter;
  private ArrayList<ProductLedgerOriginal.ProductLedgerContent> mlist;
  private CurrentUser mCurrentUser = CurrentUser.newInstance(getActivity());

  @Override public void onCreate(Bundle savedInstanceState) {
    ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.productList);
    super.onCreate(savedInstanceState);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        //                if (NavUtils.getParentActivityName(getActivity())!=null){
        //                    NavUtils.navigateUpFromSameTask(getActivity());
        //                }
        getActivity().finish();
        break;
      //强多选的产品拼接
      case R.id.menu_item_confirm:
        if (mAdapter != null) {
          getProductString();
        }
        break;

      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  private void getProductString() {
    //选择后要清空之前的数据.用新建对象的方式清空.
    ArrayList<String> codes = new ArrayList<String>();
    ArrayList<String> names = new ArrayList<String>();
    for (int i = 0; i < mAdapter.getCount(); i++) {
      LogUtil.i("mAdapterisChecked=" + i + mAdapter.isCheckedItem(i));
      if (mAdapter.isCheckedItem(i) == true) {
        LogUtil.i("选中的类型名==" + mAdapter.getItem(i).getProductName());
        codes.add(mAdapter.getItem(i).getId());
        names.add(mAdapter.getItem(i).getProductName());
      }
    }
    StringBuilder codeBuilder = new StringBuilder();
    StringBuilder nameBuilder = new StringBuilder();

    for (int i = codes.size() - 1; i >= 0; i--) {

      if (codeBuilder.length() > 0) {
        codeBuilder.append(",").append(codes.get(i));
      } else {
        codeBuilder.append(codes.get(i));
      }
      if (nameBuilder.length() > 0) {
        nameBuilder.append(",").append(names.get(i));
      } else {
        nameBuilder.append(names.get(i));
      }
    }

    Intent i = new Intent();
    i.putExtra(EXTRA_PRODUCT_CODES, codeBuilder.toString());
    i.putExtra(EXTRA_PRODUCT_NAMES, nameBuilder.toString());

    getActivity().setResult(Activity.RESULT_OK, i);
    getActivity().finish();

    LogUtil.i("codeBuilder===" + codeBuilder);
    LogUtil.i("nameBuilder===" + nameBuilder);
  }

  @Override public String getUrl() {
    return ApiConst.URL_PRODUCT_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<String, String>();
    params.put("memberCode", mCurrentUser.getMemberCode().toString());
    params.put("dataStatus", "publish");
    params.put("materialKindCodeLikeStr", "05");
    return params;
  }

  @Override public void resolveResponse(String arg0) {
    ProductLedgerOriginal original = new Gson().fromJson(arg0, ProductLedgerOriginal.class);
    List<ProductLedgerOriginal.ProductLedgerContent> rows = original.getRows();
    if (mlist == null) {
      mlist = new ArrayList<ProductLedgerOriginal.ProductLedgerContent>();
    }
    mlist.clear();
    mlist.addAll(rows);
    setAdapter(mlist);
  }

  private void setAdapter(ArrayList<ProductLedgerOriginal.ProductLedgerContent> mlist2) {
    if (null == mlist) {
      mlist = new ArrayList<ProductLedgerOriginal.ProductLedgerContent>();
    }
    // 将mtasks放入适配器显示
    mAdapter = new ProductListAdapter(getActivity(), mlist2);
    setListAdapter(mAdapter);
    ((ProductListAdapter) getListAdapter()).notifyDataSetChanged();
  }
}
